import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

import Layout from '@/layout'

export const routes = [

  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: Layout,
    hidden: true,
    children: [{
      path: '/404',
      name: '404',
      component: ()=>(import('@/views/404')),
      meta: { title: '404', icon: 'el-icon-s-help' }
    }]
  },
  {
    path: '*',
    redirect: '/404',
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'el-icon-s-help' }
    }]
  },
  {
    path: '/example',
    component: Layout,
    redirect: '/example/table',
    name: 'Example',
    meta: { title: 'Example', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'table',
        name: 'Table',
        component: () => import('@/views/dashboard/index'),
        meta: { title: 'Table', icon: 'el-icon-s-help' }
      },
      {
        path: 'tree',
        name: 'Tree',
        component: () => import('@/views/dashboard/index'),
        meta: { title: 'Tree', icon: 'el-icon-s-help' }
      }
    ]
  },

]

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}



const createRouter = () => new VueRouter({
  mode: "history",

  scrollBehavior: () => ({
    y: 0
  }),
   routes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}

export default router

